﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
using TestSystem.DataManipulation;
using TestSystem.Model.Data;
using TestSystem.Ui.Helpers;

namespace TestSystem.Ui.Forms
{
    public partial class TickForm : Form
    {
        private DateTime datetime;
        private Decimal price;
        private int volume;
        public Quote Quote { get; private set; }
        public FormType FormType { private get; set; }
        public TickForm()
        {
            InitializeComponent();
            DateTimePicker.Tag = true;
            PriceValue.Tag = false;
            VolumeValue.Tag = false;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            Text = @"Tick";
            DateTimePicker.CustomFormat = @"MM.dd.yyyy HH:mm:ss";
            SetDecimalSeparator();
            SetValues();
            if (FormType == FormType.TypeAdd)
            {
                PriceValue.Text = MessagingHelper.BeginValues;
                OkBtn.Enabled = false;
            }
            else
            {
                PriceValue.Tag = true;
                VolumeValue.Tag = true;
                
            }
        }

        private void OnValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            SetValues();
            Decimal currentvalue;
            if ((digitbox.Text != string.Empty) && (Decimal.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.InvariantCulture, out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }
        private void VolumeValueValidating(object sender, CancelEventArgs e)
        {
            var digitbox = (TextBox)sender;
            SetValues();
            Int32 currentvalue;
            if ((digitbox.Text != string.Empty) && (Int32.TryParse(digitbox.Text, NumberStyles.Any,
                 CultureInfo.InvariantCulture, out currentvalue)))
                digitbox.Tag = currentvalue > 0;
            else digitbox.Tag = false;
        }
        private void DateTimePickerValidating(object sender, CancelEventArgs e)
        {
            DateTimePicker.Tag = DateTimePicker.Value <= DateTime.Now;
        }
        private void OnValidated(object sender, EventArgs e)
        {
            OkBtn.Enabled = AreAllValuesCorrect();
        }
        private bool AreAllValuesCorrect()
        {
            return (Boolean)PriceValue.Tag && (Boolean)VolumeValue.Tag && (Boolean)DateTimePicker.Tag;
        }
        private void SetValues()
        {

            Decimal.TryParse(PriceValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                              out price);
            Int32.TryParse(VolumeValue.Text, NumberStyles.Any, CultureInfo.InvariantCulture,
                             out volume);

        }


        private void OkBtnClick(object sender, EventArgs e)
        {
            datetime = DateTimePicker.Value;
            Quote = new Quote
                        {
                            DateTime = datetime,
                            Open = null,
                            High = null,
                            Low = null,
                            Close = null,
                            Price = price,
                            Volume = volume
                        };
            DialogResult = DialogResult.OK;
            Close();
        }
        private static void TextBoxTextChanged(object sender, EventArgs e)
        {
            var digitbox = (TextBox)sender;
            digitbox.Text = digitbox.Text.Replace(',', DecimalHelper.GetDecimalSeparator());
            digitbox.Text = digitbox.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
            digitbox.SelectionStart = digitbox.Text.Length;
        }
        private void SetDecimalSeparator()
        {
            PriceValue.Text = PriceValue.Text.Replace('.', DecimalHelper.GetDecimalSeparator());
        }
    }
}
